package com.eastfair.user.service.impl;

import com.eastfair.user.dao.LikeUserMapper;
import com.eastfair.user.entity.LikeUser;
import com.eastfair.user.service.LikeUserService;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.database.mybatis.conditions.Wraps;
import com.eastfair.database.mybatis.conditions.query.QueryWrap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;

import java.util.Collection;

import org.springframework.aop.framework.AopContext;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.annotation.annotation.cache.Cache;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

import com.eastfair.user.vo.*;
import com.eastfair.user.dto.*;

/**
 * <p>
 * 业务实现类
 * 用户点赞表
 * </p>
 *
 * @author ligang
 * @date 2023-10-03
 */
@Slf4j
@Service
public class LikeUserServiceImpl extends SuperServiceImpl<LikeUserMapper, LikeUser> implements LikeUserService {


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<LikeUser> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }

    /**
     * 通用查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    public List<LikeUser> list(LikeUserDTO modelDTO, List<Long> ids) {
        return ((LikeUserService) AopContext.currentProxy()).listOfCache(modelDTO, ids);
    }


    /**
     * 缓存查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    @Cache(key = "",
            magic = @Magic(
                    key = "{" +
                            "{'userId', #args[0]?.userId}," +
                            "{'productId', #args[0]?.productId}," +
                            "{'status', #args[0]?.status}," +
                            "{'isEnabled', #args[0]?.isEnabled}," +
                            "{'id', #args[1]}" +
                            "}"))
    public List<LikeUser> listOfCache(LikeUserDTO modelDTO, List<Long> ids) {
        //查询
        LikeUser queryModel = new LikeUser();
        if (modelDTO != null) {
            BeanUtils.copyProperties(modelDTO, queryModel);
        }

        QueryWrap<LikeUser> wrapper = Wraps.q(queryModel);
        if (ids != null && !ids.isEmpty()) {
            wrapper.lambda().in(LikeUser::getId, ids);
        }
        //添加其他业务查询条件
        wrapper.lambda().eq(LikeUser::getIsDeleted, BusinessConstant.DELETE_NO);
        List<LikeUser> modelList = list(wrapper);
        return modelList;
    }


    /**
     * do转vo
     *
     * @param model model
     * @return modelVo
     */
    @Override
    public LikeUserVO doToVo(LikeUser model) {
        if (model == null) {
            return null;
        }
        LikeUserVO modelVo = new LikeUserVO();
        BeanUtils.copyProperties(model, modelVo);
        modelVo.setId(model.getId());
        return modelVo;
    }

    /**
     * do转vo
     *
     * @param modelList modelList
     * @return list
     */
    @Override
    public List<LikeUserVO> doToVos(List<LikeUser> modelList) {
        if (modelList == null || modelList.isEmpty()) {
            return null;
        }
        List<LikeUserVO> result = new ArrayList<>();
        for (LikeUser model : modelList) {
            result.add(doToVo(model));
        }
        return result;
    }
}
